/*
 * autvfmset.cpp - audio/tv/fm device configure
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "autvfmset.h"
#include "ui_autvfmset.h"

autvfmSet::autvfmSet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::autvfmSet)
{
    ui->setupUi(this);
    hide();

    message = new myMessage(0);

    getDevice = new getGstDevices(0);
    connect(getDevice,SIGNAL(finished()),this,SLOT(findDeviceGst()));
    on_updateDevices_pressed();

    settings = new QSettings("carpc", "general", this);
    settings->setPath(QSettings::IniFormat,QSettings::UserScope,"carpc");
    loadSettings();

    connect(ui->fmDevcmbBox, SIGNAL(activated(int)),this, SLOT(changeFmDev(int)));
    connect(ui->videoDevcmbBox, SIGNAL(activated(int)),this, SLOT(changeVidDev(int)));
    connect(ui->inputcmbBox, SIGNAL(activated(int)),this, SLOT(changeInDev(int)));
    connect(ui->outputcmbBox, SIGNAL(activated(int)),this, SLOT(changeOutDev(int)));

}

autvfmSet::~autvfmSet()
{
    delete ui;
}

void autvfmSet::on_updateDevices_pressed()
{
    ui->updateDevices->setDisabled(true);
    getFmDevice();
    getDevice->start();
}

void autvfmSet::loadSettings()
{
    QString devName, devPath;
    int currIndex;

    devName = settings->value("tvdevname").toString();
    devPath = settings->value("tvdevpath").toString();
    currIndex = ui->videoDevcmbBox->findText(devPath);
    if (currIndex == -1) {
        ui->videoDevcmbBox->addItem(devPath,devName);
        currIndex = 0;
    }
    ui->videoDevcmbBox->setCurrentIndex(currIndex);

    devName = settings->value("fmdevname").toString();
    devPath = settings->value("fmdevpath").toString();
    currIndex = ui->fmDevcmbBox->findText(devPath);
    if (currIndex == -1) {
        ui->fmDevcmbBox->addItem(devPath,devName);
        currIndex = 0;
    }
    ui->fmDevcmbBox->setCurrentIndex(currIndex);

    devName = settings->value("indevname").toString();
    devPath = settings->value("indevpath").toString();
    currIndex = ui->inputcmbBox->findText(devPath);
    if (currIndex == -1) {
        ui->inputcmbBox->addItem(devPath,devName);
        currIndex = 0;
    }
    ui->inputcmbBox->setCurrentIndex(currIndex);

    devName = settings->value("outdevname").toString();
    devPath = settings->value("outdevpath").toString();
    currIndex = ui->outputcmbBox->findText(devPath);
    if (currIndex == -1) {
        ui->outputcmbBox->addItem(devPath,devPath);
        currIndex = 0;
    }
    ui->outputcmbBox->setCurrentIndex(currIndex);

    updateDevName();
}

void autvfmSet::updateDevName()
{
    if(ui->videoDevcmbBox->count() != 0)
        ui->tvDeviceNameLabel->setText(ui->videoDevcmbBox->itemData(ui->videoDevcmbBox->currentIndex()).toString());
    if(ui->inputcmbBox->count() != 0)
        ui->inDevNamelabel->setText(ui->inputcmbBox->itemData(ui->inputcmbBox->currentIndex()).toString());
    if(ui->outputcmbBox->count() != 0)
        ui->outDeviceNameLabel->setText(ui->outputcmbBox->itemData(ui->outputcmbBox->currentIndex()).toString());
    if(ui->outputcmbBox->count() != 0)
        ui->fmDeviceNameLabel->setText(ui->fmDevcmbBox->itemData(ui->fmDevcmbBox->currentIndex()).toString());
}

void autvfmSet::changeInDev(int index)
{
    ui->inDevNamelabel->setText(ui->inputcmbBox->itemData(index).toString());
    settings->setValue("indevname",ui->inputcmbBox->itemData(index));
    settings->setValue("indevpath",ui->inputcmbBox->itemText(index));
}

void autvfmSet::changeOutDev(int index)
{
    ui->outDeviceNameLabel->setText(ui->outputcmbBox->itemData(index).toString());
    settings->setValue("outdevname",ui->outputcmbBox->itemData(index));
    settings->setValue("outdevpath",ui->outputcmbBox->itemText(index));
}

void autvfmSet::changeFmDev(int index)
{
    ui->fmDeviceNameLabel->setText(ui->fmDevcmbBox->itemData(index).toString());
    settings->setValue("fmdevname",ui->fmDevcmbBox->itemData(index));
    settings->setValue("fmdevpath",ui->fmDevcmbBox->itemText(index));

}

void autvfmSet::changeVidDev(int index)
{
    ui->tvDeviceNameLabel->setText(ui->videoDevcmbBox->itemData(index).toString());
    settings->setValue("tvdevname",ui->videoDevcmbBox->itemData(index));
    settings->setValue("tvdevpath",ui->videoDevcmbBox->itemText(index));
}

void autvfmSet::getFmDevice()
{
    ui->fmDevcmbBox->clear();
    QDir d("/dev/");
    d.setFilter(QDir::System);
    QStringList files = d.entryList(QStringList("*radio*"));
    foreach (QString str, files)
        ui->fmDevcmbBox->addItem("/dev/"+str,str);
    updateDevName();
}

void autvfmSet::findDeviceGst()
{
    if(getDevice->error) {
        message->exec(0,getDevice->ermessage);
        return;
    }
    ui->inputcmbBox->clear();
    ui->outputcmbBox->clear();
    ui->videoDevcmbBox->clear();
    foreach(devInfo dev, getDevice->deviceList) {
        if (dev.type == 0)
            ui->videoDevcmbBox->addItem(dev.path,dev.name);
        else if(dev.type == 1)
            ui->inputcmbBox->addItem(dev.path,dev.name);
        else if(dev.type == 2)
            ui->outputcmbBox->addItem(dev.path,dev.name);
    }
    loadSettings();
    updateDevName();
    ui->updateDevices->setDisabled(false);
}
